﻿Public Class TaxonNameCtrl

    Private _originalName As TaxonNamesInterface.Name

    Public Sub Clear()
        Me.Tag = "Edit Name"

        FullNameLabel.Text = ""
        LSIDLabel.Text = ""
        txtCanonical.Text = ""
        cmbRank.SelectedIndex = -1
        txtBasAuthors.Text = ""
        txtCombAuthors.Text = ""
        txtYearOfPub.Text = ""
        txtYearOnPub.Text = ""
        llbReference.Text = "[click to edit]"
        txtMicroreference.Text = ""
        llbTypeName.Text = "[click to edit]"
        txtTypeVoucher.Text = ""
        txtOrthography.Text = ""
        llbBasionym.Text = "[click to edit]"
        llbBasedOn.Text = "[click to edit]"
        llbReplFor.Text = "[click to edit]"
        llbHomonymOf.Text = "[click to edit]"
        llbConsAgainst.Text = "[click to edit]"
        llbBlocking.Text = "[click to edit]"
        chkIllegitimate.Checked = False
        chkInCitation.Checked = False
        chkInvalid.Checked = False
        chkMisapplied.Checked = False
        chkProParte.Checked = False
    End Sub

    Public Sub Initialise(ByVal n As TaxonNamesInterface.Name)
        Try
            Clear()

            _originalName = n.Copy

            Dim rnks As List(Of TaxonNamesInterface.Rank) = NamesClient.NamesInterfaceProxy.GetRanks()
            rnks.Insert(0, New TaxonNamesInterface.Rank)
            cmbRank.DataSource = rnks
            cmbRank.DisplayMember = "Name"
            cmbRank.ValueMember = "RankId"

            Me.Tag = "Edit Name : " + n.NameFull

            LSIDLabel.Text = n.NameLSID

            FullNameLabel.Text = n.NameFull
            txtCanonical.Text = n.Canonical
            cmbRank.SelectedValue = n.RankId
            txtBasAuthors.Text = n.BasionymAuthors
            txtCombAuthors.Text = n.CombinationAuthors
            txtYearOfPub.Text = n.YearOfPublication
            txtYearOnPub.Text = n.YearOnPublication
            If n.Reference IsNot Nothing Then
                llbReference.Text = n.Reference.Citation
            End If
            txtMicroreference.Text = n.MicroReference
            If n.TypeName IsNot Nothing Then
                llbTypeName.Text = n.TypeName.NameFull
            End If
            txtTypeVoucher.Text = n.TypeVoucher
            txtOrthography.Text = n.Orthography
            If n.Basionym IsNot Nothing Then
                llbBasionym.Text = n.Basionym.NameFull
            End If
            If n.BasedOn IsNot Nothing Then
                llbBasedOn.Text = n.BasedOn.NameFull
            End If
            If n.ReplacementFor IsNot Nothing Then
                llbReplFor.Text = n.ReplacementFor.NameFull
            End If
            If n.HomonymOf IsNot Nothing Then
                llbHomonymOf.Text = n.HomonymOf.NameFull
            End If
            If n.ConservedAgainst IsNot Nothing Then
                llbConsAgainst.Text = n.ConservedAgainst.NameFull
            End If
            If n.BlockingName IsNot Nothing Then
                llbBlocking.Text = n.BlockingName.NameFull
            End If

            chkIllegitimate.Checked = n.Illegitimate
            chkInCitation.Checked = n.InCitation
            chkInvalid.Checked = n.Invalid
            chkMisapplied.Checked = n.Misapplied
            chkProParte.Checked = n.ProParte

        Catch ex As Exception
            TaxonNamesInterface.NamesException.LogError(ex)
            MsgBox("Failed to load name")
        End Try

    End Sub

    Private Sub llbReference_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles llbReference.LinkClicked
        Try
            Dim selRef As New SelectReferenceForm
            If selRef.ShowDialog = DialogResult.OK Then
                _originalName.ReferenceId = selRef.SelectedReferenceId
                _originalName.Reference = Nothing
                llbReference.Text = selRef.SelectedReferenceText
            End If
        Catch ex As Exception
            TaxonNamesInterface.NamesException.LogError(ex)
            MsgBox("Failed to link reference")
        End Try
    End Sub
End Class
